<?php

class Category_MobileController extends Ac_Controller_Mobile {

    private $category;
    
    public function init() {
        /* Initialize action controller here */
        $this->_helper->layout->setLayout('layout-mobile2');
    }

    public function preDispatch() {

        //$this->_helper->_layout->setLayout('layout');

        $level3 = $this->_request->getParam('level3');
        $level2 = $this->_request->getParam('level2');
        $level1 = $this->_request->getParam('level1');

        $lang = $this->view->current_language;

        if ($this->_request->getActionName() !== 'view-all' && $this->_request->getActionName() !== 'disclamer') {
            $level_num = 3;
            if ($level1 !== null) {
                $level_num = 1;
                $cat1 = Model_Category::one(array('url_en' => $level1, 'path' => array('$size' => ($level_num))));
                if (empty($cat1)) {
                    $cat1 = Model_Category::one(array('url_tc' => $level1, 'path' => array('$size' => ($level_num))));
                    $lang = 'tc';
                }
                $level = $level1;
            }

            if ($level2 !== null) {
                $level_num = 2;
                //$level = $this->_request->getParam('level2');
                $cat2 = Model_Category::one(array('url_en' => $level2, 'path' => array('$all' => array($cat1->_id), '$size' => $level_num)));
                if (empty($cat2)) {
                    $cat2 = Model_Category::one(array('url_tc' => $level2, 'path' => array('$all' => array($cat1->_id), '$size' => $level_num)));
                    //$cat2 = Model_Category::one(array('url_tc' => $level2, 'path' => $cat1->_id, 'path' => array('$size' => $level_num)));
                    $lang = 'tc';
                }
                $level = $level2;
            }

            if ($level3 !== null) {
                $level_num = 3;
                //$level = $this->_request->getParam('level2');
                //$cat3 = Model_Category::one(array('url_en' => $level3, 'path' => $cat2->_id, 'path' => array('$size' => $level_num)));
                $cat3 = Model_Category::one(array('url_en' => $level3, 'path' => array('$all' => array($cat2->_id), '$size' => $level_num)));
                if (empty($cat3)) {
                    //$cat3 = Model_Category::one(array('url_tc' => $level3, 'path' => $cat2->_id, 'path' => array('$size' => $level_num)));
                    $cat3 = Model_Category::one(array('url_tc' => $level3, 'path' => array('$all' => array($cat2->_id), '$size' => $level_num)));
                    $lang = 'tc';
                }
                $level = $level3;
            }

            $this->view->level = $level;

            $session = new Zend_Session_Namespace('current_language');
            if (!empty($cat3)) {
                $category = $cat3;
            } elseif (!empty($cat2)) {
                $category = $cat2;
            } elseif (!empty($cat1)) {
                $category = $cat1;
            }
            $parent = $category->getParent();
            if (!empty($parent)) {
                $parent = Model_category::find($parent->_id);
            }

            //var_dump($lang, $cat1->nm_en, $cat2->nm_en, $cat3->nm_en, $category->nm_en, (string)$cat2->_id, (string)$parent->nm_en);exit;
            //$category = Model_Category::one(array('url_en' => $level, 'path' => array('$size' => ($level_num))));
            if (empty($category)) {//not english
                $category = Model_Category::one(array('url_tc' => $level, 'path' => array('$size' => ($level_num))));

                if (!empty($category)) {//not english
                    $this->category = $category;
                    $session->current_language = 'tc';
                    $this->_request->setParam('category', $category);
                }
                //let it slide
            } else {
                $this->category = $category;
                $session->current_language = $lang;
                $this->_request->setParam('category', $category);
            }
        }

        $this->_setParam('lang', $lang);
        parent::preDispatch();
        $this->_helper->layout->setLayout('layout-mobile2');
        //var_dump($this->_request);exit;
    }

    public function indexAction() {
        $name_en = ($this->current_language == 'tc') ? "url_tc" : "url_en";
        $level3 = $this->_request->getParam('level3');
        $level2 = $this->_request->getParam('level2');
        $level1 = $this->_request->getParam('level1');
        //$this->view->level1 = $level1;
        //echo ">>>>>>>>>>>>lvl1: ".$level1." | lvl2: ".$level2." | lvl3: ".$level3;

        if ($level3 !== null) {
            $lvl3 = Model_Category::getLevel3(array($name_en => $level3, 'status' => Model_Category::STATUS_ACTIVE));
            foreach ($lvl3 as $mc) {
                break;
            }
            if (empty($mc)) {
                //	echo "kill"; exit();
                throw new Ac_Exception_MissingListing('not found');
            }
            $mc_temp = $mc->getChildren(false, array('status' => Model_Category::STATUS_ACTIVE, 'display_website' => 1))->sort(array('nm_en' => 1));
            if ($mc_temp->count() > 0) {
                //echo $mc_temp->count();exit();
                $this->view->mc = $mc_temp;
                $this->view->mc_name = $mc->name;

                //$this->_helper->layout->setLayout('layout-mobile');
                //$this->view->main_content = "mobile/view-level.phtml";
                //$this->view->module_content = "category";
                $this->render('view-level');
            } else {
                //echo $mc->name;exit();	
                Zend_Registry::set('category_list_id', (string) $mc->_id);
                $_GET['small_category_list'] = (string) $mc->_id;
                return $this->_forward('regular', 'mobile', 'search');
            }
        } else {
            if ($level2 !== null) {
                $lvl2 = Model_Category::getLevel2(array($name_en => $level2, 'status' => Model_Category::STATUS_ACTIVE));
                $mc_temp = null;
                foreach ($lvl2 as $mc) {
                    break;
                }

                if (empty($mc)) {
                    throw new Ac_Exception_MissingListing('not found');
                }
                $mc_temp = $mc->getChildren(false, array('status' => Model_Category::STATUS_ACTIVE, 'display_website' => 1))->sort(array('nm_en' => 1));
                if (is_object($mc_temp) && $mc_temp->count() > 0) {
                    $this->view->mc = $mc_temp;
                    $this->view->mc_name = $mc->name;
                    $this->render('view-level');
                } else {
                    Zend_Registry::set('category_list_id', (string) $mc->_id);
                    $_GET['small_category_list'] = (string) $mc->_id;
                    return $this->_forward('regular', 'mobile', 'search');
                }
            } else {
                if ($level1 !== null) {
                    $lvl1 = Model_Category::getLevel1(array($name_en => $level1, 'status' => Model_Category::STATUS_ACTIVE));
                    foreach ($lvl1 as $mc) {
                        break;
                    }
                    if (empty($mc)) {
                        //	echo "kill"; exit();
                        throw new Ac_Exception_MissingListing('not found');
                    }
                    $mc_temp = $mc->getChildren(false, array('status' => Model_Category::STATUS_ACTIVE, 'display_website' => 1))->sort(array('nm_en' => 1));

                    if ($mc_temp->count() > 0) {
                        //echo $mc_temp->count();exit();
                        $this->view->mc = $mc_temp;
                        $this->view->mc_name = $mc->name;

                        //$this->_helper->layout->setLayout('layout-mobile');
                        //$this->view->main_content = "mobile/view-level.phtml";
                        //$this->view->module_content = "category";
                        $this->render('view-level');
                    } else {
                        //echo "SERP";exit();	
                        Zend_Registry::set('category_list_id', (string) $mc->_id);
                        $_GET['small_category_list'] = (string) $mc->_id;
                        return $this->_forward('regular', 'mobile', 'search');
                    }
                } else {
                    //$this->_helper->layout->setLayout('layout-mobile');
                    //$this->view->main_content = "mobile/view-all.phtml";
                    //$this->view->module_content = "category";
                    //$this->render('view-all');
                    $this->_forward('view-all', 'mobile', 'category');
                }
            }
        }
    }

    public function viewAllAction() {
        $this->render('view-all');
    }

}